package cn.adviceclient.config.impl;

import cn.adviceclient.AdviceClient;
import cn.adviceclient.config.Config;
import cn.adviceclient.features.value.AbstractSetting;
import cn.adviceclient.features.value.BooleanSetting;
import cn.adviceclient.features.value.ModeSetting;
import cn.adviceclient.features.value.StringSetting;
import cn.adviceclient.features.value.slider.DoubleSetting;
import cn.adviceclient.features.value.slider.FloatSetting;
import cn.adviceclient.features.value.slider.IntSetting;
import cn.adviceclient.features.value.slider.NumberSetting;
import cn.adviceclient.features.module.Module;
import cn.adviceclient.features.module.ModuleManager;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import java.util.List;

public class ModuleConfig extends Config {
    public ModuleConfig(String name) {
        super(name);
    }

    @Override
    public void loadConfig(JsonObject jsonObject) {
        try {
            // 遍历所有模块
            for (Module module : ModuleManager.getModules()) {
                JsonElement moduleElement = jsonObject.get(module.getName());

                if (moduleElement != null && moduleElement.isJsonObject()) {
                    JsonObject moduleObj = moduleElement.getAsJsonObject();

                    // 加载模块启用状态
                    if (moduleObj.has("enabled")) {
                        boolean enabled = moduleObj.get("enabled").getAsBoolean();
                        if (enabled != module.isEnabled()) {
                            module.toggle(); // 只有当状态不同时才切换
                        }
                    }

                    // 加载模块按键绑定
                    if (moduleObj.has("keyBind")) {
                        module.bindKey(moduleObj.get("keyBind").getAsInt());
                    }

                    // 加载模块设置
                    if (moduleObj.has("settings") && moduleObj.get("settings").isJsonObject()) {
                        JsonObject settingsObj = moduleObj.getAsJsonObject("settings");

                        for (AbstractSetting<?> setting : getModuleSettings(module)) {
                            if (settingsObj.has(setting.getName())) {
                                JsonElement settingElement = settingsObj.get(setting.getName());

                                if (setting instanceof BooleanSetting) {
                                    ((BooleanSetting) setting).setValue(settingElement.getAsBoolean());
                                } else if (setting instanceof NumberSetting) {
                                    try {
                                        if (setting instanceof IntSetting) {
                                            ((IntSetting) setting).setValue(settingElement.getAsInt());
                                        } else if (setting instanceof FloatSetting) {
                                            ((FloatSetting) setting).setValue(settingElement.getAsFloat());
                                        } else if (setting instanceof DoubleSetting) {
                                            ((DoubleSetting) setting).setValue(settingElement.getAsDouble());
                                        }
                                    } catch (Exception e) {
                                        // 忽略转换错误
                                    }
                                } else if (setting instanceof ModeSetting) {
                                    ((ModeSetting) setting).setValue(settingElement.getAsString());
                                } else if (setting instanceof StringSetting) {
                                    ((StringSetting) setting).setValue(settingElement.getAsString());
                                }
                            }
                        }
                    }
                }
            }
            AdviceClient.LOGGER.info("Configuration '{}' loaded successfully", getName());
        } catch (Exception e) {
            AdviceClient.LOGGER.error("Failed to load configuration '{}': {}", getName(), e.getMessage());
            e.printStackTrace();
        }
    }

    @Override
    public JsonObject saveConfig() {
        JsonObject root = new JsonObject();

        // 遍历所有模块
        for (Module module : ModuleManager.getModules()) {
            JsonObject moduleObj = new JsonObject();

            // 保存模块启用状态
            moduleObj.addProperty("enabled", module.isEnabled());

            // 保存模块按键绑定
            moduleObj.addProperty("keyBind", module.getKeyBind());

            // 保存模块设置
            List<AbstractSetting<?>> settings = getModuleSettings(module);
            if (!settings.isEmpty()) {
                JsonObject settingsObj = new JsonObject();

                for (AbstractSetting<?> setting : settings) {
                    if (setting instanceof BooleanSetting) {
                        settingsObj.addProperty(setting.getName(), ((BooleanSetting) setting).getValue());
                    } else if (setting instanceof NumberSetting) {
                        settingsObj.addProperty(setting.getName(), ((NumberSetting<?>) setting).getValue().toString());
                    } else if (setting instanceof ModeSetting) {
                        settingsObj.addProperty(setting.getName(), ((ModeSetting) setting).getValue());
                    } else if (setting instanceof StringSetting) {
                        settingsObj.addProperty(setting.getName(), ((StringSetting) setting).getValue());
                    }
                }

                moduleObj.add("settings", settingsObj);
            }

            // 将模块添加到根对象
            root.add(module.getName(), moduleObj);
        }

        return root;
    }
    
    /**
     * 获取模块的所有设置
     */
    private List<AbstractSetting<?>> getModuleSettings(Module module) {
        return module.getSettings();
    }
}